<?php
$wl = 250;

include ('../Connections/KRAPPERCON.php');
//require('../Libs/fpdf/font/dot.php');
require('../Libs/fpdf/fpdf.php');
require('../Libs/fpdf/extend.php');

	$type = '';
	$docid = '';
	$cliente = '';
	$vendedor = '';
	$fecha ='';
	$cladoc = '';
	$cldoc = '';
	$hora = '';
	$hora  = '';
	$direccion = '';
	$ruc = '';
	$fol = '';
	
$oper = $_GET['oper'];
//$oper = 'print';

if ($oper == 'print'){

$coti = $_GET['documento'];
//$coti  = '180000055';

$query="call sp_printCoti('".$coti."');";
//echo $query;

$res = $mysqli->query($query);
if ($res) {
	if ($row = $res->fetch_assoc() ){
	$docid = $row['pedido'];	
	$cliente = $row['nomcli'];
	$vendedor = $row['usrnom'];
	$cladoc = substr($row['cladoc'],0,2);
	if ($cladoc == 'PD'){
		$tipo = 'PEDIDO';
		$field = 'DetPedQta';
	}
	elseif ($cladoc == 'CT'){
		$tipo = 'COTIZACION';	
		$field = 'DetPedQtp';		
	}

	
	$fecha = $row['fecped'];
	$hora = $row['hora'];
	//echo $hora;
	//$hora  = '';
	$direccion = $row['dircli'];
	$ruc = $row['nifcli'];
	//$fol = $row['folio'];
	}
	$res->close();
	$mysqli->check_result();
}

$sx = 5;
$fnt = 'Dot';
$pdf=new PDF('L','mm','A5');
$pdf->AddPage();
$pdf->AddFont('Dot','','dot.php');
$pdf->SetMargins(5,5,5,5);
$pdf->SetFont($fnt,'',10);
$pdf->SetAutoPageBreak(true, 0); 

$pdf->Ln(-34);
$pdf->SetX(220);
$pdf->Cell(0,5, $fecha, 0, 1, 'R');
$pdf->SetX($sx);
$pdf->Cell(0, 5, "REPUESTOS", 0,0,'L');

//Creamos una celda de 10 de ancho, 20 de alto, con ese texto en su interior, y 1 de borde.
$pdf->Cell(0,5, $tipo." / ".$docid , 0, 1, 'R');
$pdf->Ln(2);
$pdf->SetX($sx);
$pdf->Cell(0, 0, "", 1, 2, 'L', true);

$pdf->Ln(2);
$pdf->SetX($sx);
$pdf->Cell(20,5, "Cliente:", 0, 0, 'R');
$pdf->SetX($sx + 20);
$pdf->Cell(100,5, $cliente,0 , 0, 'L');
$pdf->SetX($sx);
$pdf->Cell(0,5, "HORA : "." ".$hora, 0, 1, 'R');
$pdf->SetX($sx);
$pdf->Cell(20,5, "N.I.F.", 0, 0, 'R');
$pdf->SetX($sx);
$pdf->Cell(0,5, "Vendedor : ". $vendedor, 0, 0, 'R');

$pdf->SetX($sx + 20);
$pdf->Cell(100,5, $ruc, 0, 1, 'L');
$pdf->SetX($sx);
$pdf->Cell(20,5, "Direccion:", 0, 0, 'R');
$pdf->SetX($sx + 20);
$pdf->Cell(100,5, $direccion, 0, 1, 'L');


$pdf->Ln(2);

$pdf->SetX($sx);
$pdf->Cell(0, 0, "", 1, 2, 'L', true);
$pdf->SetX($sx);
			    $pdf->SetX($sx);
//				$pdf->SetXY($sx);
				//$pdf->Row(array('Almacen', 'Articulo', 'Cant.', 'I.G.V.','VVU S/.','VVT S/.'),'M',-1,0,6);
				
			//    $pdf->Cell(30,5, "Numero de Parte", 0, 0, 'C');
				$pdf->Cell(14,5, "Alm.", 0, 0, 'C');
				$pdf->Cell(70,5, "Articulo", 0, 0, 'C');
				$pdf->Cell(20,5, "Cantidad", 0, 0, 'C');
				$pdf->Cell(20,5, "I.G.V", 0, 0, 'C');
				$pdf->Cell(40,5, "VVU S/.", 0, 0, 'C');
				$pdf->Cell(40,5, "VVT S/.", 0, 1, 'C');
		
$pdf->SetX($sx);
$pdf->Cell(0, 0, "", 1, 2, 'L', true);
$pdf->SetX($sx);		
$pdf->Ln(2);			

$neto1 = 0;
$neto2 = 0;
$neto = 0;
$pru = 0;
$dscto = 0 ;
$query = "call sp_getPedidoDet('".$coti."')";
$res4 = $mysqli->query($query) or die($mysqli->error.__LINE__);
$igv = 0;
$igv_por = 18;
if ($res4) {

while ($row3 = $res4->fetch_assoc() )
			
			{
			    $pdf->SetX($sx);
				$pdf->SetFont($fnt,'',10);
				
			    $pdf->Cell(14,5, $row3['AlmacenID'], 0, 0, 'C');
//			    $pdf->Cell(30,5, $row3['ArticuloID'], 0, 0, 'C');
				$pdf->Cell(70,5, $row3['DetPedTxt'],0, 0, 'L');
				$pdf->Cell(20,5, $row3[$field], 0, 0, 'C');
				//$pdf->Cell(25,5, $row3['DetFactDct'], 0, 0, 'C');
				$pdf->Cell(20,5, $igv_por, 0, 0, 'C');
				$pdf->Cell(40,5, number_format($row3['DetPedNet']/ $row3[$field],2), 0, 0, 'C');
				$pdf->Cell(40,5, $row3['DetPedNet'], 0, 1, 'C');
				
				//$pdf->Row(array($row3['AlmacenID'],  $row3['DetPedTxt'], $row3[$field], $igv_por,number_format($row3['DetPedNet']/ $row3[$field],2),$row3['DetPedNet']),'M',-1,0,6);
				
				
	         	//$igv2=$row3['DetPedIva'];
				$igv+=$row3['DetPedIva'];
				//$neto1= $neto1 + ( $row3['DetPedQta'] * $row3['DetPedPru'] );
				//$pru = $pru + $row3['DetFactPru'];
				$neto2 = $neto2 + $row3['DetPedNet'];
				$dscto = $dscto+$row3['DetPedDcto'];
					
			}
$res4->close();
$mysqli->check_result();
			
			}

	$pdf->SetY(130);
    $pdf->SetX(25);

	//$pn = $neto-$dscto;	
	//$igv =($pn) *($igv/100);
	$tot =$neto2  +$igv ;
					$pdf->SetFont($fnt,'',10);
					
	$query2="call sp_getNroletras ($tot,'Nuevos Soles');";
$pdf->SetX($sx);
$res2 = $mysqli->query($query2) or die($mysqli->error.__LINE__);
if ($res2) {
	if($row2 = $res2->fetch_assoc()) {			
    $pdf->Cell(0,5, $row2['res'], 0, 1, 'L');
	}
	$res2->close();
$mysqli->check_result();
}
	$pdf->SetX($sx);
	$pdf->Cell(0, 0, "", 1,1, 'L', true);
    $pdf->SetX($sx);		
//$pdf->Cell(1,5, "VVP Neto S/.:", 0, 0, 'R');
//$pdf->Cell(24,5, number_format($neto1,2), 0, 0, 'L');
//$pdf->Cell(24,5, "Dscto S/.:", 0, 0, 'R');
//$pdf->Cell(24,5, number_format($dscto,2), 0, 0, 'L');
$pdf->Cell(24,5, "V.V.P S/.:", 0, 0, 'R');
$pdf->Cell(24,5,number_format($neto2,2)  , 0, 0, 'L');
$pdf->Cell(50,5, "I.G.V ".$igv_por." S/.", 0, 0, 'R');
$pdf->Cell(30,5, number_format($igv,2) , 0, 0, 'L');

$pdf->SetFont($fnt,'B',10);
$pdf->Cell(40,5, "P.V.P S/.:", 0, 0, 'R');
$pdf->Cell(30,5,number_format($tot,2) , 0, 1, 'L');

    $pdf->SetX($sx);
	$pdf->Cell(0, 0, "", 1, 1, 'L', true);
    $pdf->SetX($sx);
	/*
$pdf->Cell(50,5, "P.V.P S/.:", 0, 0, 'R');
$pdf->Cell(50,5,number_format( $tot,2), 0, 1, 'R');
*/





$pdf->Output($tipo.'_'.$docid,'I');

}


?>